package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private AttrValueService attrValueService;

    @Autowired
    private FacilityInfoService facilityInfoService;

    @Autowired
    private LabelInfoService labelInfoService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;



    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Override
    public IPage<RoomItemVo> getRoomItemVoList(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.getRoomItemVoList(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailVo(Long id) {
        RoomInfo roomInfo = this.getById(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        List<GraphVo> graphVoList =graphInfoService.getGraphVoListByRoomId(id);
        List<AttrValueVo> attrValueVoList = attrValueService.getAttrValueVoListByRoomId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoService.getFacilityInfoListByRoomId(id);
        List<LabelInfo> labelInfoList = labelInfoService.getLabelInfoListByRoomId(id);
        List<PaymentType> paymentTypeList = paymentTypeService.getPaymentTypeListByRoomId(id);
        List<LeaseTerm> leaseTermList = leaseTermService.getLeaseTermListByRoomId(id);


        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;

    }

    @Override
    public void saveOrUpdateRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        //先拿id看有没有
        this.saveOrUpdate(roomSubmitVo);
        //这里会自动创建id
        if(id != null){
            //改
            //先删Vo中的数据
            //删中间表
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
        }

        //id不为空就是增
        //增不用删 直接将传过来的数据保存
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfos.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfos);


        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
        attrValueIds.forEach(attrValueId -> {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                    .roomId(roomSubmitVo.getId())
                    .attrValueId(attrValueId).build();
            roomAttrValues.add(roomAttrValue);
        });
        roomAttrValueService.saveBatch(roomAttrValues);

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
        facilityInfoIds.forEach(facilityInfoId -> {
            RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
            roomFacilities.add(roomFacility);
        });
        roomFacilityService.saveBatch(roomFacilities);

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        ArrayList<RoomLabel> roomLabels = new ArrayList<>();
        labelInfoIds.forEach(labelInfoId -> {
            RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
            roomLabels.add(roomLabel);
        });
        roomLabelService.saveBatch(roomLabels);


        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        paymentTypeIds.forEach(paymentTypeId -> {
            RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
            roomPaymentTypes.add(roomPaymentType);
        });
        roomPaymentTypeService.saveBatch(roomPaymentTypes);


        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        leaseTermIds.forEach(leaseTermId -> {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
            roomLeaseTerms.add(roomLeaseTerm);
        });
        roomLeaseTermService.saveBatch(roomLeaseTerms);
    }

    @Override
    public void removeRoomInfoById(Long id) {
        super.removeById(id);
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id));
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
    }


}




